package com.feihong.yw189.activity.kuandai;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.feihong.yw189.R;
import com.feihong.yw189.activity.base.BaseActivity;
import com.feihong.yw189.activity.kaika.KaiKaActivity;
import com.feihong.yw189.activity.yonghuzhongxin.MySettingActivity;
import com.feihong.yw189.adapter.Kuandai.EditBaoZhuangAdapter;
import com.feihong.yw189.app.App;
import com.feihong.yw189.bluetooth.dialog.MyUsualyDialog;
import com.feihong.yw189.listener.http.RequestCallBack;
import com.feihong.yw189.message.LoginMessage;
import com.feihong.yw189.model.Kaika.BaozhuangEditModel;

import org.xutils.common.Callback;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 手输报装地址
 */

public class BaoZhuangEditActivity extends BaseActivity {

    @BindView(R.id.rl_goback)
    RelativeLayout rlGoback;
    @BindView(R.id.iv_lin)
    ImageView ivLin;
    @BindView(R.id.txt_pagetitle)
    TextView txtPagetitle;
    @BindView(R.id.et_input)
    EditText minput;
    @BindView(R.id.iv_search)
    ImageView ivsearch;
    @BindView(R.id.rl_choose_number)
    RelativeLayout rl_btn;
    @BindView(R.id.grid)
    GridView gridView;
    @BindView(R.id.iv_submit)
    ImageView ivSubmit;

    @BindView(R.id.connect_net_error_layout)
    LinearLayout mConnectNetErrorLayout;

    @BindView(R.id.ll_one)
    LinearLayout mContentInputLayout;
    @BindView(R.id.broadband_input_adr_scrollview)
    ScrollView mContentScrollView;
    private String getInput;
    private EditBaoZhuangAdapter madapter;
    private ArrayList<BaozhuangEditModel.DataBean.ListBean> mlistBean;
    private int gvPosition = 0;
    MyUsualyDialog progressDialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editbaozhuang);
        ButterKnife.bind(this);

        initView();
        initData();
        bindData();
    }

    @Override
    public void setTheme() {

    }

    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    private void initView() {
        progressDialog = new MyUsualyDialog(this, R.layout.wait_item);
        progressDialog.setCanceledOnTouchOutside(true);
        progressDialog.setCancelable(false);
        txtPagetitle.setText("报装地址");
        ivLin.setVisibility(View.GONE);
        minput.setHint("请输入地址");

        rl_btn.setVisibility(View.GONE);
        mlistBean = new ArrayList<>();

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                madapter.changeState(position);
                gvPosition = position;
            }
        });
    }

    private void initData() {
        //网络请求
        progressDialog.show();
        progressDialog.setWarningTxt(R.id.progress_layout_warning_tv,"地址加载中");
        App.getApp().getHttpTools().getEditBaozhuang(new RequestCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                if (activityIsDestory()) {
                    return;
                }
                try {
                    BaozhuangEditModel model = JSON.parseObject(result, BaozhuangEditModel.class);
                    if (model != null) {
                        if (model.getStatus() == 1) {
                            if (model.getData().getList().size() > 0) {
                                if(mContentInputLayout.getVisibility() == View.GONE){
                                    mContentInputLayout.setVisibility(View.VISIBLE);
                                    mContentScrollView.setVisibility(View.VISIBLE);
                                    ivSubmit.setVisibility(View.VISIBLE);
                                }
                                BaozhuangEditModel.DataBean.ListBean listBean;
                                for (BaozhuangEditModel.DataBean.ListBean mbean : model.getData().getList()) {
                                    listBean = new BaozhuangEditModel.DataBean.ListBean();
                                    listBean.setDire_name(mbean.getDire_name());
                                    listBean.setDire_no(mbean.getDire_no());
                                    mlistBean.add(listBean);
                                }
                                madapter = new EditBaoZhuangAdapter(BaoZhuangEditActivity.this, R.layout.item_gridview, mlistBean);
                                gridView.setAdapter(madapter);
                            } else {
                                showToast("没有数据");
                            }
                        } else {
                            showToast(model.getInfo());
                        }
                    } else {
                        showToast("数据出错,请重试");
                    }
                } catch (Exception ex) {
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if(activityIsDestory()){return;}
                mConnectNetErrorLayout.setVisibility(View.VISIBLE);
                mContentInputLayout.setVisibility(View.GONE);
                mContentScrollView.setVisibility(View.GONE);
                ivSubmit.setVisibility(View.GONE);
            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {
            }

            @Override
            public void onFinished() {
                if(activityIsDestory()){return;}
                progressDialog.dismiss();
            }
        });
    }

    public void bindData() {
    }

    @OnClick({R.id.rl_goback, R.id.iv_submit, R.id.requery_data_tv})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.rl_goback:
                finish();
                break;
            case R.id.iv_submit:
                getInput = minput.getText().toString().trim().replaceAll("\n", "");
                if (!TextUtils.isEmpty(getInput)) {
                    try {
                        if (mlistBean != null && mlistBean.size() > 0) {
                            if (!gridView.isFocused()) {
                                Intent mIntent = new Intent();
                                Bundle bundle = new Bundle();
                                bundle.putString("Dire_name", mlistBean.get(gvPosition).getDire_name());
                                bundle.putString("Dire_no", mlistBean.get(gvPosition).getDire_no());
                                bundle.putString("inputadress", getInput);
                                mIntent.putExtras(bundle);
                                mIntent.setClass(BaoZhuangEditActivity.this, KaiKaActivity.class);
                                setResult(RESULT_OK, mIntent);
                                finish();
                            }
                        }
                    } catch (Exception ex) {
                    }
                } else {
                    showToast("请输入地址");
                }
                break;
            case R.id.requery_data_tv:
                mConnectNetErrorLayout.setVisibility(View.GONE);
                initData();
                break;
            default:
                break;
        }
    }

}

